﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Cost.Models;
using Microsoft.Practices.ServiceLocation;
using NBlock.Core;
using System.Collections.Generic;
using System.Collections;

namespace Cost.Services
{
    /// <summary>
    /// 枚举服务类，用于获取下拉框的数据源
    /// </summary>
    public class nbEnumService : IEnumService
    {
        /// <summary>
        /// 获取指定枚举Key的下拉枚举数据源
        /// </summary>
        /// <param name="EnumKey">枚举Key</param>
        /// <returns>枚举数据源</returns>
        public IList<DicItem> GetEnumDataSource(string enumKey)
        {
            var enumType = GetEnumType(enumKey);
            if (enumType == null)
            {
                var items = new List<DicItem>();
                if (enumKey == "dictype")
                {
                    var repo = ServiceLocator.Current.GetInstance<ICostRepository<EnumType>>();
                    items = repo.FindAll().OrderBy(t => t.SeqNo)
                        .Select(type => new DicItem { Text = type.TypeName, Value = type.TypeCode })
                        .ToList();
                }
                else if (enumKey == "Material")
                {
                    var repo = ServiceLocator.Current.GetInstance<ICostRepository<Material>>();
                    items = repo.FindAll().OrderBy(t => t.Name)
                        .Select(m => new DicItem { Text = m.Name, Value = m.Id })
                        .ToList();
                }
                else if (enumKey == "Supplier")
                {
                    var repo = ServiceLocator.Current.GetInstance<ICostRepository<Supplier>>();
                    items = repo.FindAll().OrderBy(t => t.Name)
                        .Select(m => new DicItem { Text = m.Name, Value = m.Id })
                        .ToList();
                }
                else if (enumKey == "ProjectSite")
                {
                    var repo = ServiceLocator.Current.GetInstance<ICostRepository<ProjectSite>>();
                    items = repo.FindAll().OrderBy(t => t.Name)
                        .Select(s => new DicItem { Text = s.Name, Value = s.Id })
                        .ToList();
                }
                else
                {
                    var repo = ServiceLocator.Current.GetInstance<ICostRepository<EnumItem>>();
                    items = repo.FindAll(item => item.TypeCode == enumKey).OrderBy(t => t.SeqNo)
                        .Select(item => new DicItem { Text = item.EnumName, Value = item.EnumCode })
                        .ToList();
                }
                return items;
            }
            else
            {
                return GetEnumDataSource(enumType);
            }

        }

        /// <summary>
        /// 获取枚举的数据集合
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <returns></returns>
        private IList<DicItem> GetEnumDataSource(Type enumType)
        {
            var values = Enum.GetValues(enumType);
            var items = new List<DicItem>();
            foreach (var value in values)
            {
                var text = string.Empty;
                MemberInfo[] memInfo = enumType.GetMember(value.ToString());
                if (memInfo != null && memInfo.Length > 0)
                {
                    var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (attrs != null && attrs.Length > 0)
                    {
                        text = ((DescriptionAttribute)attrs[0]).Description;
                    }
                }
                items.Add(new DicItem { Value = value.ToString(), Text = text });
            }

            return items;
        }

        private Type GetEnumType(string fullName)
        {
            if (fullName.Contains('.'))
            {
                var asmName = fullName.Substring(0, fullName.LastIndexOf('.'));
                return Type.GetType(fullName + "," + asmName, false, true);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取指定枚举项值的枚举项文本
        /// </summary>
        /// <param name="enumKey">枚举Key</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public string GetEnumText(string enumKey, string value)
        {
            var selectedItem = GetEnumDataSource(enumKey).Where(item => item.Value == value).FirstOrDefault();
            if (selectedItem != null)
            {
                return selectedItem.Text;
            }
            else
            {
                return string.Empty;
            }
        }

        public string GetEnumValue(string enumKey, string text)
        {
            var selectedItem = GetEnumDataSource(enumKey).Where(item => item.Text == text).FirstOrDefault();
            if (selectedItem != null)
            {
                return selectedItem.Value;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
